package com.example.lenovo.demo.model;

import android.content.Context;
import android.util.Log;

import com.example.lenovo.demo.MyApplication;
import com.example.lenovo.demo.model.databean.CityBean;
import com.example.lenovo.demo.model.databean.CityFirstHotBean;
import com.example.lenovo.demo.model.databean.CityFirstHotTabmsgBean;
import com.example.lenovo.demo.model.databean.CityHotBean;
import com.example.lenovo.demo.model.databean.HeaderBean;
import com.example.lenovo.demo.model.databean.HomeBean;
import com.example.lenovo.demo.model.databean.MainMapBean;
import com.example.lenovo.demo.model.databean.ModelDaysBean;
import com.example.lenovo.demo.model.databean.StrategyBean;
import com.example.lenovo.demo.model.databean.TravelsBean;
import com.example.lenovo.demo.model.idatabean.OnCityFirstHotSuccess;
import com.example.lenovo.demo.model.idatabean.OnCityFirstHotTabMsgSuccess;
import com.example.lenovo.demo.model.databean.UserBean;
import com.example.lenovo.demo.model.idatabean.IModel;
import com.example.lenovo.demo.model.idatabean.OnCityHotSuccess;
import com.example.lenovo.demo.model.idatabean.OnCitySuccess;
import com.example.lenovo.demo.model.idatabean.OnMainMapSuccess;
import com.example.lenovo.demo.model.idatabean.OnModelDaysSuccess;
import com.example.lenovo.demo.model.idatabean.OnStrategySuccess;
import com.example.lenovo.demo.model.idatabean.OnHeaderSuccess;
import com.example.lenovo.demo.model.idatabean.OnHomeSuccess;
import com.example.lenovo.demo.model.idatabean.OnTravelsSuccess;
import com.example.lenovo.demo.model.iretrofit.CityFirstHotMsgRF;
import com.example.lenovo.demo.model.iretrofit.CityFirstHotRF;
import com.example.lenovo.demo.model.idatabean.OnUserSuccess;
import com.example.lenovo.demo.model.iretrofit.CityHotRF;
import com.example.lenovo.demo.model.iretrofit.CityRF;
import com.example.lenovo.demo.model.iretrofit.HeaderRF;
import com.example.lenovo.demo.model.iretrofit.HomeRF;
import com.example.lenovo.demo.model.iretrofit.MainMapRF;
import com.example.lenovo.demo.model.iretrofit.ModelDaysRF;
import com.example.lenovo.demo.model.iretrofit.StrategyRF;
import com.example.lenovo.demo.model.iretrofit.TravelsRF;
import com.example.lenovo.demo.model.iretrofit.UserRF;
import com.example.lenovo.demo.model.uri.Uri;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Lenovo on 2017/1/6.
 */

public class Model implements IModel {
    private DbManager db = x.getDb(MyApplication.getDaoConfig());
    private List<HeaderBean.DataBean> data_Header;
    private List<HomeBean.DataBean> data_Home;
    private List<TravelsBean.DataBean> data_Travels;
    private List<UserBean.DataBean> data_User;
    private List<StrategyBean> data_Strategy;

    @Override
    public void sendHeaderBean(final Context context, final OnHeaderSuccess onHeaderSuccess) {
        data_Header = new ArrayList<>();
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()
                .create(HeaderRF.class)
                .getData(Uri.HEADER)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HeaderBean>() {
                    @Override
                    public void call(HeaderBean headerBean) {
                        if (headerBean.getData() != null) {
                            data_Header.addAll(headerBean.getData());
                            onHeaderSuccess.HeaderSuccess(data_Header);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onHeaderSuccess.HeaderFailed();
                    }
                });
    }

    @Override
    public void sendHomeBean(Context context, final OnHomeSuccess onHomeSuccess) {
        data_Home = new ArrayList<>();
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()
                .create(HomeRF.class)
                .getData(Uri.HOME)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HomeBean>() {
                    @Override
                    public void call(final HomeBean homeBean) {
                        if (homeBean.getData() != null) {
                            for (HomeBean.DataBean dataBean : homeBean.getData()) {
                                switch (dataBean.getRegion()) {
                                    case "china":
                                        for (HomeBean.DataBean.DestinationsBean bean : dataBean.getDestinations()) {
                                            bean.setType("china");
                                        }
                                        break;
                                    case "hk":
                                        for (HomeBean.DataBean.DestinationsBean bean : dataBean.getDestinations()) {
                                            bean.setType("hk");
                                        }
                                        break;
                                    case "asia":
                                        for (HomeBean.DataBean.DestinationsBean bean : dataBean.getDestinations()) {
                                            bean.setType("asia");
                                        }
                                        break;
                                    case "europe":
                                        for (HomeBean.DataBean.DestinationsBean bean : dataBean.getDestinations()) {
                                            bean.setType("europe");
                                        }
                                        break;
                                    case "other":
                                        for (HomeBean.DataBean.DestinationsBean bean : dataBean.getDestinations()) {
                                            bean.setType("other");
                                        }
                                        break;

                                }
                            }

                            data_Home.addAll(homeBean.getData());
                            onHomeSuccess.HomeSuccess(data_Home);

                            /**
                             * 下载完毕数据，顺便保存到数据库
                             * */
                            new Thread(){
                                @Override
                                public void run() {
                                    super.run();
                                    try {
                                        db.delete(HomeBean.DataBean.class);
                                        db.delete(HomeBean.DataBean.DestinationsBean.class);
                                        db.delete(HomeBean.DataBean.DestinationsBean.PhotoBean.class);
                                        db.saveOrUpdate(homeBean);
                                        for (HomeBean.DataBean dataBean : homeBean.getData()) {
                                            db.saveOrUpdate(dataBean);
                                            for (HomeBean.DataBean.DestinationsBean destinationsBean : dataBean.getDestinations()) {
                                                db.saveOrUpdate(destinationsBean);
                                            }
                                        }
                                    } catch (DbException e) {
                                        e.printStackTrace();
                                    }


                                }
                            };
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
//                        List<HomeBean.DataBean> dbHomeBeans=null;
//                        try {
//
//
//                            dbHomeBeans=db.selector(HomeBean.DataBean.class).findAll();
//                            for (int i = 0; i < dbHomeBeans.size(); i++) {
//                                dbHomeBeans.get(i).setDestinations(db.selector(HomeBean.DataBean.DestinationsBean.class)
//                                        .where("type","=","china")
//                                        .findAll()
//                                );
//                            }
//
//                            data_Home.addAll(dbHomeBeans);
//
//
//
//                        } catch (DbException e) {
//                            e.printStackTrace();
//                        }


                        onHomeSuccess.HomeFailed(data_Home);
                    }
                });
    }

    @Override
    public void sendCityBean(String id, final OnCitySuccess onCitySuccess) {
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()
                .create(CityRF.class)
                .getData(Uri.CITYHEADER + id + Uri.CITYBOTTOM)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CityBean>() {
                    @Override
                    public void call(CityBean cityBean) {
                        if (cityBean != null) {
                            onCitySuccess.CitySuccess(cityBean);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onCitySuccess.CityFailed();
                    }
                });

    }

    @Override
    public void sendTravelsBean(Context context, final OnTravelsSuccess onTravelsSuccess) {
        data_Travels = new ArrayList<>();
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(TravelsRF.class).getData(Uri.TRAVELS)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<TravelsBean>() {
                    @Override
                    public void call(TravelsBean travelsBean) {
                        if (travelsBean.getData() != null) {
                            data_Travels.addAll(travelsBean.getData());
                            onTravelsSuccess.TravelsSuccess(data_Travels);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onTravelsSuccess.TravelsFailed();
                    }
                });
    }

    @Override
    public void sendCityHotBean(String area, final OnCityHotSuccess onCityHotSuccess) {
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(CityHotRF.class)
                .getData(Uri.CITY_HOT + area)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CityHotBean>() {
                    @Override
                    public void call(CityHotBean cityHotBean) {
                        onCityHotSuccess.CityHotSuccess(cityHotBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onCityHotSuccess.CityHotFailed();
                    }
                });

    }

    @Override
    public void sendUserBean(String id, final OnUserSuccess onUserSuccess) {
        Log.i("161101", "sendUserBean: "+id);
        data_User = new ArrayList<>();
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(UserRF.class).getData(Uri.USERHEADER + id + Uri.USERFOOT)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserBean>() {
                    @Override
                    public void call(UserBean userBean) {
                        Log.i("161101", "call: "+userBean.getData().toString());
                        data_User.add(userBean.getData());
                        onUserSuccess.UserSuccess(data_User);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.i("161101", "call: xxxxxxx");
                        onUserSuccess.UserFailed();
                    }
                });
    }

    @Override
    public void sendModelDaysBean(String id, final OnModelDaysSuccess onModelDaysSuccess) {
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(ModelDaysRF.class)
                .getData(Uri.DAYSHEAD+id+Uri.DAYSBOTTOM)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ModelDaysBean>() {
                    @Override
                    public void call(ModelDaysBean modelDaysBean) {
                        onModelDaysSuccess.ModelDaysSuccess(modelDaysBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    @Override
    public void sendMainMapBean(String id, final OnMainMapSuccess onMainMapSuccess) {
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(MainMapRF.class)
                .getData(Uri.MAINMAPHEAD+id+Uri.MAINMAPBOTTOM)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<MainMapBean>() {
                    @Override
                    public void call(MainMapBean mainMapBean) {
                        onMainMapSuccess.MainMapSuccess(mainMapBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    @Override
    public void sendStrategyBean(String id, final OnStrategySuccess onStrategySuccess) {
        data_Strategy=new ArrayList<>();
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(StrategyRF.class)
                .getData(Uri.STRATEGYHEAD+id+Uri.STRATEGYBOTTOM)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<StrategyBean>>() {
                    @Override
                    public void call(List<StrategyBean> strategyBeen) {
                        data_Strategy.addAll(strategyBeen);
                        Log.e("msg",data_Strategy.size()+"");
                        onStrategySuccess.StrategySuccess(data_Strategy);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onStrategySuccess.StrategyFaild();
                    }
                });

    }

    @Override
    public void sendCityFirstHotBean(String id, final OnCityFirstHotSuccess onCityFirstHotSuccess) {
        new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(CityFirstHotRF.class)
                .getData(Uri.CITYFIRSTHOT+id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CityFirstHotBean>() {
                    @Override
                    public void call(CityFirstHotBean cityFirstHotBean) {
                        onCityFirstHotSuccess.CityFirstHotsuccess(cityFirstHotBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    @Override
    public void sendCityFirstHotMsg(String id, final OnCityFirstHotTabMsgSuccess onCityFirstHotTabMsgSuccess) {new Retrofit.Builder().baseUrl(Uri.MAIN_SERVICE)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .build().create(CityFirstHotMsgRF.class)
            .getData(Uri.CITYFIRSTHOTTABMSGHEAN+id+Uri.CITYFIRSTHOTTABMSGBOTTOM)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<CityFirstHotTabmsgBean>() {
                @Override
                public void call(CityFirstHotTabmsgBean cityFirstHotTabmsgBean) {
                    onCityFirstHotTabMsgSuccess.OnCityFirstHotTabMsgSuccess(cityFirstHotTabmsgBean);
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {

                }
            });
    }

}
